package com.sound.service;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.hoangkien.util.JSONParser;
import com.hoangkien.util.StreamUtils;
import com.hoangkien.util.TestWifiConnected;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class GetStringSearchCloud extends AsyncTask<String, String, JSONObject>{
	private static final String TAG = "GetStringSearchCloud";
	private Context context;
	private Boolean isConnectionExist = false; // test wifi
	private TestWifiConnected cd;
	
	private String url = "";
	private String jsonString;
	
	public GetStringSearchCloud(Context context, String keysearch){
		this.context = context;
		this.url = "http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q="
				+ keysearch + "&format=5&alt=json";
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}
	
	@Override
	protected JSONObject doInBackground(String... params) {
		cd = new TestWifiConnected(context);
		isConnectionExist = cd.checkMobileInternetConnection();
		if(isConnectionExist){  //co Internet
			JSONParser jparser = new JSONParser();
			jsonString = jparser.getJSONFromUrl(url);
			try {
				 HttpClient client = new DefaultHttpClient();
				 HttpUriRequest request = new HttpGet(url);
				 HttpResponse response = client.execute(request);
				 String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
				 JSONArray jsonArr = new JSONArray(jsonString);
				 
			} catch (Exception e) {
				Log.e(TAG, e.toString());
			}
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(JSONObject result) {
		super.onPostExecute(result);
	}

}
